home *** CD-ROM | disk | FTP | other *** search
- SHUFFLE.PAS COMPLETE LISTING
-
-
- PROGRAM demonstrate_shuffling;
- VAR
- deck : ARRAY[1..52] OF Integer; {or of anything else}
- saved, i, j : 1..52;
-
- BEGIN
- {place initial values in array}
- FOR i := 1 TO 52 DO
- deck[i] := i;
-
- {seed random number generator}
- Randomize;
-
- {here's the guts of it}
-
- {shuffle the array - pass *once* through the array, swapping each element
- with another, randomly chosen, element}
-
- FOR i := 1 TO 52 DO
- BEGIN
- {find another element to swap with. Random returns value between
- 0 and 51, so must add 1}
- j := Random(52)+1;
- {make sure not swap element with itself}
- WHILE j = i DO
- j := Random(52)+1;
- {perform the swap}
- saved := deck[i];
- deck[i] := deck[j];
- deck[j] := saved
- END;
-
- {output for testing purposes}
- FOR i := 1 TO 52 DO
- BEGIN
- Write(deck[i]:3);
- IF i MOD 10 = 0 THEN WriteLn;
- END;
- WriteLn;
-
- END.
-